<?php 
	$pPageIsPublic = false;
	include '../_common.php';
	$_SESSION["m1"]="destination";
	$_SESSION["m2"]="destination-list";
	
	
	$pTitle  = 'New Accomodation';
	$pButton = 'Save';
	$action  = 'add';

	
	if (isset($_GET['rid'])){
		$objRegion = new Region();
		if (! $objRegion ->loadByKey('regionId', intval($_REQUEST['rid']))) {
			unset($_SESSION['rid']);
			Tzn::redirect('region-add.php', 'Error: Region no exist.', 1);	
		}
		$_SESSION['rid'] = $objRegion ->id;	
	} else {
		Tzn::redirect('region-add.php', 'Error: Region no exist.', 1);
	}
	
	$objAc = new Accomodation();
	if (isset($_GET['aid'])){
		if (! $objAc ->loadByFilter(
				$objAc ->getIdKey(). "=". intval($_GET['aid']) .
				" AND ". $objAc ->gTable(). ".regionId = ". $_SESSION['rid'])) {
			unset($_SESSION['aid']);
		} else {
			$pTitle  = 'Edit Accomodation';
			$pButton = 'Update';
			$action  = 'edit';
			$_SESSION['aid'] = $objAc->id;
		}
	}
	
	if ($_GET['action']== 'del') {
		if ($objAc ->isLoaded()) {
			$objAc ->removeImage('bgImage');
			$msg = "Picture has been deleted.";
		}
	}
	
	if (isset($_POST['submit'])) {
		$objAc ->setAuto($_POST);
		
		$objAc ->uploadImage('ifile', 'bgImage');
		$objAc ->region ->id = $_SESSION['rid'];
//$objAc ->locationMap = arr2json($_POST, array('metaTitle', 'metaDescription', 'metaKeyword', 'friendlyUrl'));
		if ($_POST['action'] == 'edit') {
			$objAc ->getConnection();
			$objAc ->setUid($_SESSION['aid']);
			$objAc ->update();
			Tzn::redirect($_SERVER['PHP_SELF']."?rid=". $_SESSION['rid'] ."&aid=". $objAc ->id, 'Accomodation has been updated.');
		} elseif ($_POST['action'] == 'add') {
			$objAc ->add();
			Tzn::redirect($_SERVER['PHP_SELF']."?rid=". $_SESSION['rid'] ."&aid=". $objAc ->id, 'Accomodation has been added.');
		}
	}
	
	if (isset($_POST['divingcenterId'])){
		$dc = intval($_POST['divingcenterId']);
		$objTmp = new DivCenterAccomodation();
		if (! $objTmp ->loadByFilter($objTmp ->gTable().'.accomodationId='. $objAc ->id. " AND " .
			$objTmp ->gTable().'.divingcenterId='. $dc)) {
			$objTmp ->accomodation ->id = $objAc ->id;
			$objTmp ->divingcenter ->id = $dc;
			$objTmp ->add();
			Tzn::redirect($_SERVER['PHP_SELF']."?rid=". $_SESSION['rid'] ."&aid=". $objAc ->id.'&tab=divcenter', 'Diving Center has been added.');
		} else {
			Tzn::redirect($_SERVER['PHP_SELF']."?rid=". $_SESSION['rid'] ."&did=". $objAc ->id.'&tab=divcenter', 'Diving Center exist on list.');
		}
	}
	
	
?>
<?php include("header.php"); ?>
<?php
$tabs 	= array('divcenter' => 1);
$selTab = $tabs[$_REQUEST['tab']] ? $tabs[$_REQUEST['tab']] : 0;
?>
<script type="text/javascript">
$(document).ready(function(){
	$('#frmMain').validate();
	$('#content-tabs').tabs({
		disabled :[<?php echo !$objAc ->isLoaded() ? "2" : "" ?>],
		initial  : <?php echo $selTab ?>  
	});
		
	$('ul.lt1').admin_sortable("divcenteraccomodation");
	
	$('#select-all1').checkedToggle('ul.lt1');
	
	$('#del').live('click', function(){
		var check = $(this).closest("li").find('input:checkbox');  
		check.attr('checked', true);
		delItemSelected(check, $(this).attr('class'));
	});	
});

function delItemSelected(btn, cls) {
	var checked = (btn.id == "checked") ? $('ul.'+ $(btn).attr('class')).find('input:checked') : $(btn).closest('ul').find('input:checked');
	
	
	if (checked.length == 0) {
		Notify('Select a Item.!',1);
		return false;
	} else {
		var items, li;
		if (btn.id == "checked") {
			items = checked.serialize();
			li = checked.closest("li");
		} else {
			items = $(btn).serialize();
			li = $(btn).closest("li");
		}
		
		items = decodeURIComponent(items);
		
		if (! confirm("You going to delete "+ li.length +" items ?")){
			return false;
		} else {
			ajaxAdmin({
				data: items + "&action=item-delete&class="+ cls,
				success: function(response) {
					if (response) {
						Notify(response, 0);
						//li.fadeOut(300, function(){$(this).remove();});
						setTimeout('refresh()',500);
					}	
				}
			});
		}					
	}
}	

function refresh(){
	location.reload(true);
}

</script>
<?php include("sidebar.php"); ?>
<ul id="subnav" class="bar" >
				<li ><a href="destination-list.php" > Country List </a></li>
				<li><font>&raquo;</font></li>
				<li ><a href="destination-add.php?id=<?php echo $objRegion ->country ->id ?>&tab=region" > Region List </a></li>
				<li><font>&raquo;</font></li>
				<li ><a href="region-add.php?id=<?php echo $objRegion ->country ->id ?>&rid=<?php echo $objRegion ->id ?>&tab=accomodation" > Accomodation List </a></li>
				<li><font>&raquo;</font></li>
				<li ><span><?php echo $pTitle;?></span></li>
</ul>
<div id="list">
	<h2> <?php echo $pTitle;?> </h2>
    <div id="content-tabs">
		<ul>
			<li><a href="#fragment-1"><span>Accomodation Detail</span></a></li>
			<li><a href="#fragment-2"><span>Diving Centers </span></a></li>
		</ul>
		<div id="fragment-1" >
			<form action="" method="post" name = "frmMain" id = "frmMain"  enctype="multipart/form-data" >
			<fieldset><legend> General Info </legend>
					<table cellpadding="0" cellspacing="0" class="table-form"  >
						<tr>
							<th width="12%" >Title: </th>
							<td width="88%" ><?php $objAc ->qText('title', '', 'width:548px', 'class="required"');?></td>
						</tr>
						<!--tr>
							<th>Sub Title: </th>
							<td><?php $objAc ->qText('subTitle', '', 'width:548px', 'class=""');?></td>
						</tr-->
						<tr>
						<td colspan="2" >
						<br/>
							<label>Description: </label><?php $objAc ->qEditor('description', '', $config ); ?></td>
						</tr>
						<?php $objAc ->printSEO()  ?>						
						<tr>
							<th>Picture : </th>
							<td>
						<?php	if ($objAc ->bgImage): ?>
								<div style="width: 230px;" class="img-border">
									<img alt="" src="../thumb.php?src=<?php echo TZN_FILE_UPLOAD_URL_ADMIN.$objAc ->bgImage?>&w=230&h=80" width="230" height="80"   ><br/>
									<a href="<?php echo $_SERVER['PHP_SELF']  ?>?action=del&rid=<?php echo $objAc ->region ->id ?>&aid=<?php echo $objAc ->id ?>"  >delete picture</a>
								</div>
							<?php
								else:
									$objAc ->qFile('ifile', '', '', 'class="required"'); 		
								endif; 
							?>
							</td>
						</tr>
						<tr>
							<th>Status: </th>
							<td><?php $objAc ->arrayToSelect($objAc ->_arrStatus, 'status', $objAc ->status, false, 'width:100px', '');?></td>
						</tr>
					</table>
			</fieldset>
			<div style="margin-top:10px">
				<input type="hidden" name="action" value="<?php echo $action; ?>" />
                <input type="submit" name="submit"  value="<?php echo $pButton; ?>" /> &nbsp; <input type="reset" value="Cancel" onClick="window.location.href='region-add.php?id=<?php echo $objAc ->region ->country ->id ?>&rid=<?php echo $objAc ->region ->id ?>&tab=divcenter'" />
            </div>
            </form>
		</div>
		<div id="fragment-2" >
		<script type="text/javascript">
			$().ready(function(){
				$('form#dvs-to-list').validate();
			});
			
		</script>
		<form id="dvs-to-list" action="?rid=<?php echo $objAc ->region ->id  ?>&aid=<?php echo $objAc ->id  ?>&tab=divcenter" method="post" style="margin-top: -7px;" >
			<ul id="subnav" >
	 			<li class="noleft" ><input type="checkbox" name="select-all1" id="select-all1" /><label for="select-all1" class="first-check" >Select All</label></li>
				<li class="noborder" ><a href="javascript:void(0);" class="lt1"  id="checked" onclick="delItemSelected(this, 'divcenteraccomodation')"  >Delete Selection</a></li>
				<li class="noborder"><span>Diving Centers : </span>
				<?php	
					$da = new DivCenterAccomodation();
					$da ->addWhere($da ->gTable().'.accomodationId='. $objAc ->id);
					
					$objDc = new DivingCenter();
					$c = @implode(",",getRecordsByColum($da, "divingcenterId"));
					$objDc ->loadItems(null, '#status = "Live" ' . ($c ? 
											 'AND #divingcenterId  NOT IN ('.$c.') ' : '') .     
											 ' AND #regionId ='. $objAc ->region ->id);	
					Generic::gSelect($objDc, '', '', '-- select a diving center --', '', 'class="required"');
				?>			
				<a href="javascript:void(0)" onclick="$('form#dvs-to-list').submit();"  class="add" title="Add to list" >&nbsp; </a>
				</li>
			</ul>
		</form>
		<table id="gradient-style" >
			<thead>
				<tr>
		    		<th width="76%" >Diving Center</th>
		    		<th width="20%" >Action</th>
		  		</tr>
			</thead>
		</table>
		<ul id="listin" class="lt1"  >
		<?php
		$objAccom = new DivCenterAccomodation();
		$objAccom  ->addOrder('orderId ASC');
		$objAccom ->addWhere($objAccom ->gTable().'.accomodationId = '. $objAc ->id);
		$objAccom ->loadList();
		if ($objAccom ->rMore()):
			while($objItem = $objAccom ->rNext()):
		?>
			<li id="recordsArray_<?php echo $objItem->id; ?>" ><table id="gradient-style">	
				<tr>
					<td width="4%" ><input type="checkbox" name="items[]" value="<?php echo $objItem->id;?>" /></td>
		    		<td width="76%" ><strong><?php echo $objItem ->divingcenter->title  ?></strong></td>
		    		<td width="20%" >
		    			<p class="subnav">
			    			<a class="divcenteraccomodation"  id="del"  href="javascript:void(0)"> delete from list    </a>
			    		</p>    		
		    		</td>
		  		</tr></table>
		  	</li>
	<?php 	endwhile;
		endif;  ?>  		
		</ul>
		</div> 
	</div>
</div>
<?php include("footer.php"); ?>